﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using JumpingBalls.AppCode;

namespace JumpingBalls
{
    public partial class MainPage
    {
        private readonly GameContext context = App.Context;

        private readonly Block[,] blocks = new Block[GameContext.GroundLength, GameContext.GroundLength];

        private Block tappedBlock;

        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            LayoutInit();

            listTops.ItemsSource = Tops.TopsList;

            context.OnGroundValueChange += context_OnGroundValueChange;

            this.LayoutRoot.DataContext = context;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Check TombStone
            if(context.IsRuning)
            {
                for (int i = 0; i < GameContext.GroundLength; i++)
                {
                    for (int j = 0; j < GameContext.GroundLength; j++)
                    {
                        context.ChangeValue(i,j, context.Ground[i][j], false);
                    }
                }
            }
        }

        private void btnStartGame_Click(object sender, RoutedEventArgs e)
        {
            var msgRst = MessageBox.Show("Begin A New Game", "Start Game", MessageBoxButton.OKCancel);

            if (msgRst == MessageBoxResult.OK)
            {
                LayoutInit();

                Game.StartGame();
            }
        }

        private void LayoutInit()
        {
            tappedBlock = null;

            canvasBlocks.Width = Block.BlockLength * GameContext.GroundLength;
            canvasBlocks.Height = Block.BlockLength * GameContext.GroundLength;

            // Ground Init
            for (int row = 0; row < GameContext.GroundLength; row++)
            {
                for (int col = 0; col < GameContext.GroundLength; col++)
                {
                    Block block = new Block(row, col);

                    block.Tap += block_Tap;

                    blocks[row, col] = block;

                    block.SetValue(Canvas.LeftProperty, (double)(col * Block.BlockLength));
                    block.SetValue(Canvas.TopProperty, (double)(row * Block.BlockLength));
                    canvasBlocks.Children.Add(block);
                }
            }
        }

        private void block_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Block block = sender as Block;

            if(block == tappedBlock)
            {
                block.ChangeBallStoryBoard();
            }
            else if (context.Ground[block.Row][block.Col] != 0)
            {
                if (tappedBlock != null)
                {
                    tappedBlock.StopBallStoryBoard();
                }

                block.StartBallStoryBoard();

                tappedBlock = block;
            }
            else if (context.Ground[block.Row][block.Col] == 0)
            {
                if(tappedBlock != null)
                {
                    tappedBlock.StopBallStoryBoard();

                    Game.MoveBall(tappedBlock.Row, tappedBlock.Col, block.Row, block.Col);

                    tappedBlock = null;
                }
            }

            if(context.BlockLeft == 0)
            {
                popTops.IsOpen = true;
            }
        }

        private void context_OnGroundValueChange(int row, int col, int value, bool isEraseBall)
        {
            blocks[row, col].ChangeBallBrush(value, isEraseBall);
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            if(popTops.IsOpen)
            {
                popTops.IsOpen = false;

                e.Cancel = true;

                return;
            }

            var msgRst = MessageBox.Show("Exist Game？", "Exist Game", MessageBoxButton.OKCancel);

            if (msgRst == MessageBoxResult.Cancel)
            {
                e.Cancel = true;
            }
        }

        private void btnVote_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/About.xaml", UriKind.Relative));
        }

        private void btnViewTops_Click(object sender, RoutedEventArgs e)
        {
            popTops.IsOpen = true;
        }

        private void btnPopBlack_Click(object sender, RoutedEventArgs e)
        {
            popTops.IsOpen = false;
        }
    }
}